//
//  LGFollowDetailOpinionCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/6/26.
//

#import "LGFollowDetailOpinionCell.h"
#import "LGFollowRecordItemView.h"
#import "MCPlayer.h"
@interface LGFollowDetailOpinionCell()<MCPlayerDelegate,LGFollowRecordItemDelegate>
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *opinionTitle;
@property (nonatomic , strong)UILabel *opinionLabel;
@property (nonatomic , strong)UIView *opinionView;
/**   */
@property (nonatomic , strong)UILabel *planTitle;
@property (nonatomic , strong)UILabel *planLabel;
@property (nonatomic , strong)UIView *planView;
/**   */
@property (nonatomic , strong)UILabel *focusTitle;
@property (nonatomic , strong)UILabel *focusLabel;
@property (nonatomic , strong)UIView *focusView;

/**   */
@property (nonatomic , strong)NSMutableArray *itemArray;
/**   */
@property (nonatomic , assign)NSInteger playIndex;

@end

@implementation LGFollowDetailOpinionCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    NSString *opinionStr = LGNSString(dataDic[@"handle1"][@"content"]);
    NSString *planStr = LGNSString(dataDic[@"handle2"][@"content"]);
    NSString *focusStr = LGNSString(dataDic[@"handle3"][@"content"]);
    CGFloat opinionTop = (opinionStr.length > 0)?viewPix(12):0;
    CGFloat planTop = (planStr.length > 0)?viewPix(12):0;
    CGFloat focusTop = (focusStr.length > 0)?viewPix(12):0;
    self.opinionLabel.text = opinionStr;
    self.planLabel.text = planStr;
    self.focusLabel.text = focusStr;
    [self.opinionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.opinionLabel.mas_bottom).offset(opinionTop);
    }];
    [self.planView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.planLabel.mas_bottom).offset(planTop);
    }];
    [self.focusView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.focusLabel.mas_bottom).offset(focusTop);
    }];
    
    NSArray *opinionArray = dataDic[@"handle1"][@"audioes"];
    NSArray *planArray = dataDic[@"handle2"][@"audioes"];
    NSArray *focusArray = dataDic[@"handle3"][@"audioes"];
    
    for (UIView *itemView in self.itemArray) {
        [itemView removeFromSuperview];
    }
    [self.itemArray removeAllObjects];
    CGFloat opinTop = 0;
    for (NSInteger i=0; i<opinionArray.count; i++) {
        LGFollowRecordItemView *itemView = [[LGFollowRecordItemView alloc]initWithFrame:CGRectMake(0, opinTop, viewPix(175), viewPix(32))];
        itemView.audioUrl = LGNSString(opinionArray[i][@"url"]);
        itemView.duration = LGNSString(opinionArray[i][@"duration"]);
        itemView.canDelete = NO;
        itemView.delegate = self;
        itemView.index = self.itemArray.count;
        [self.opinionView addSubview:itemView];
        [self.itemArray addObject:itemView];
        NSString *content = LGNSString(opinionArray[i][@"text"]);
        CGFloat textHeight = [content textHeightWithFont:LGFont(14) maxWidth:Screen_W-viewPix(64)];
        UILabel *textLabel = [UILabel lableWithText:content colorString:@"#666666" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:0];
        textLabel.frame = CGRectMake(0, kMaxY(itemView.frame)+viewPix(8), Screen_W-viewPix(64), textHeight+5);
        [self.opinionView addSubview:textLabel];
        [self.itemArray addObject:textLabel];
        opinTop = kMaxY(textLabel.frame)+viewPix(8);
    }
    [self.opinionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(opinTop));
    }];
    
    //方案调整描述
    CGFloat descTop = 0;
    for (NSInteger i=0; i<planArray.count; i++) {
        LGFollowRecordItemView *itemView = [[LGFollowRecordItemView alloc]initWithFrame:CGRectMake(0, descTop, viewPix(175), viewPix(32))];
        itemView.audioUrl = LGNSString(planArray[i][@"url"]);
        itemView.duration = LGNSString(planArray[i][@"duration"]);
        itemView.canDelete = NO;
        itemView.delegate = self;
        itemView.index = self.itemArray.count;
        [self.planView addSubview:itemView];
        [self.itemArray addObject:itemView];
        NSString *content = LGNSString(planArray[i][@"text"]);
        CGFloat textHeight = [content textHeightWithFont:LGFont(14) maxWidth:Screen_W-viewPix(64)];
        UILabel *textLabel = [UILabel lableWithText:content colorString:@"#666666" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:0];
        textLabel.frame = CGRectMake(0, kMaxY(itemView.frame)+viewPix(8), Screen_W-viewPix(64), textHeight+5);
        [self.planView addSubview:textLabel];
        [self.itemArray addObject:textLabel];
        descTop = kMaxY(textLabel.frame)+viewPix(8);
    }
    [self.planView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(descTop));
    }];
    
    CGFloat focuTop = 0;
    for (NSInteger i=0; i<focusArray.count; i++) {
        LGFollowRecordItemView *itemView = [[LGFollowRecordItemView alloc]initWithFrame:CGRectMake(0, focuTop, viewPix(175), viewPix(32))];
        itemView.audioUrl = LGNSString(focusArray[i][@"url"]);
        itemView.duration = LGNSString(focusArray[i][@"duration"]);
        itemView.canDelete = NO;
        itemView.delegate = self;
        itemView.index = self.itemArray.count;
        [self.focusView addSubview:itemView];
        [self.itemArray addObject:itemView];
        NSString *content = LGNSString(focusArray[i][@"text"]);
        CGFloat textHeight = [content textHeightWithFont:LGFont(14) maxWidth:Screen_W-viewPix(64)];
        UILabel *textLabel = [UILabel lableWithText:content colorString:@"#666666" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:0];
        textLabel.frame = CGRectMake(0, kMaxY(itemView.frame)+viewPix(8), Screen_W-viewPix(64), textHeight+5);
        [self.focusView addSubview:textLabel];
        [self.itemArray addObject:textLabel];
        focuTop = kMaxY(textLabel.frame)+viewPix(8);
    }
    [self.focusView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(focuTop));
    }];
}

-(void)playVoiceWithUrl:(NSString *)url index:(NSInteger)index{
    self.playIndex = index;
    for (NSInteger i = 0; i<self.itemArray.count; i++) {
        LGFollowRecordItemView *itemView = self.itemArray[i];
        if([itemView isKindOfClass:[LGFollowRecordItemView class]]){
            itemView.playing = (i == index) ? YES:NO;
        }
    }
    MCPlayer *player = [MCPlayer shareManager];
    [player playMediaWithUrl:url tempPath:nil desPath:nil delegate:self];
    player.delegate = self;
}

-(void)playerPlayTimeSecond:(CGFloat)seconds duration:(CGFloat)duration currentStr:(NSString *)currentString withResidueStr:(NSString *)residueStr player:(NSObject *)player{
    if(seconds == duration){
        LGFollowRecordItemView *itemView = self.itemArray[self.playIndex];
        itemView.playing = NO;
    }
}

-(void)playerStop{
    for (NSInteger i = 0; i<self.itemArray.count; i++) {
        LGFollowRecordItemView *itemView = self.itemArray[i];
        if([itemView isKindOfClass:[LGFollowRecordItemView class]]){
            itemView.playing = NO;
        }
    }
}

-(void)createSubView{
    self.clipsToBounds = YES;
    self.contentView.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
    [self.contentView addSubview:self.baseView];
    [self.baseView addSubview:self.opinionTitle];
    [self.baseView addSubview:self.opinionLabel];
    [self.baseView addSubview:self.opinionView];
    [self.baseView addSubview:self.planTitle];
    [self.baseView addSubview:self.planLabel];
    [self.baseView addSubview:self.planView];
    [self.baseView addSubview:self.focusTitle];
    [self.baseView addSubview:self.focusLabel];
    [self.baseView addSubview:self.focusView];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(5));
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.right.equalTo(self.contentView).offset(-viewPix(16));
        make.bottom.equalTo(self.contentView).offset(-viewPix(7));
    }];
    [self.opinionTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.baseView).offset(viewPix(16));
        make.right.equalTo(self.baseView).offset(-viewPix(16));
    }];
    [self.opinionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.opinionTitle.mas_bottom).offset(viewPix(8));
        make.left.right.equalTo(self.opinionTitle);
    }];
    [self.opinionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.opinionLabel.mas_bottom).offset(viewPix(8));
        make.left.right.equalTo(self.opinionTitle);
        make.height.equalTo(@(0));
    }];
    [self.planTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.opinionView.mas_bottom).offset(viewPix(10));
        make.left.right.equalTo(self.opinionTitle);
    }];
    [self.planLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.planTitle.mas_bottom).offset(viewPix(8));
        make.left.right.equalTo(self.opinionTitle);
    }];
    [self.planView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.planLabel.mas_bottom).offset(viewPix(8));
        make.left.right.equalTo(self.opinionTitle);
        make.height.equalTo(@(0));
    }];
    [self.focusTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.planView.mas_bottom).offset(viewPix(10));
        make.left.right.equalTo(self.opinionTitle);
    }];
    [self.focusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.focusTitle.mas_bottom).offset(viewPix(8));
        make.left.right.equalTo(self.opinionTitle);
    }];
    [self.focusView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.focusLabel.mas_bottom).offset(viewPix(8));
        make.left.right.equalTo(self.opinionTitle);
        make.height.equalTo(@(0));
        make.bottom.equalTo(self.baseView).offset(-viewPix(16));
    }];
    
}

-(NSMutableArray *)itemArray{
    if(!_itemArray){
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B2BDC7" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}
-(UILabel *)opinionTitle{
    if(!_opinionTitle){
        _opinionTitle = [UILabel lableWithText:@"本次随访意见" colorString:@"#333333" textFont:LGFontWeight(15, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _opinionTitle;
}
-(UILabel *)opinionLabel{
    if(!_opinionLabel){
        _opinionLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _opinionLabel;
}
-(UIView *)opinionView{
    if(!_opinionView){
        _opinionView = [[UIView alloc] init];
    }
    return _opinionView;
}

-(UILabel *)planTitle{
    if(!_planTitle){
        _planTitle = [UILabel lableWithText:@"方案调整描述" colorString:@"#333333" textFont:LGFontWeight(15, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _planTitle;
}
-(UILabel *)planLabel{
    if(!_planLabel){
        _planLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _planLabel;
}
-(UIView *)planView{
    if(!_planView){
        _planView = [[UIView alloc] init];
    }
    return _planView;
}

-(UILabel *)focusTitle{
    if(!_focusTitle){
        _focusTitle = [UILabel lableWithText:@"下次随访关注点" colorString:@"#333333" textFont:LGFontWeight(15, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _focusTitle;
}
-(UILabel *)focusLabel{
    if(!_focusLabel){
        _focusLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _focusLabel;
}
-(UIView *)focusView{
    if(!_focusView){
        _focusView = [[UIView alloc] init];
    }
    return _focusView;
}




- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
